package com.example.huhaichao.fmusic.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.huhaichao.fmusic.R;
import com.example.huhaichao.fmusic.model.MusicModel;
import com.example.huhaichao.fmusic.utils.MusicList;

import java.util.ArrayList;

/**
 * Created by huhaichao on 2017/9/29.
 */

public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.ViewHolder> {
    private ArrayList<MusicModel> musicList;
    private onItemClickListener onItemClickListener;
    public MusicListAdapter() {
        this.musicList = MusicList.getMusicList();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.music_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.name.setText(musicList.get(position).getMusicName());
        holder.artist.setText(musicList.get(position).getMusicArtist());
        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(position);
            }
        });
    }

    /**
     * 回调方法
     */
    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 回调接口
     */
    public interface onItemClickListener {
        void onItemClick(int i);
    }

    @Override
    public int getItemCount() {
        return musicList.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout item;
        private TextView name;
        private TextView artist;
        public ViewHolder(View view) {
            super(view);
            item = (LinearLayout) view.findViewById(R.id.item);
            name= (TextView) view.findViewById(R.id.name);
            artist = (TextView) view.findViewById(R.id.artist);
        }
    }
}
